package cn.krui.helper.tools;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Map;

/**
 * @author zc
 */
public class JsonUtils {
  public static String toJson(Object data) {
    StringWriter writer = new StringWriter();
    ObjectMapper mapper = new ObjectMapper();
    try {
      mapper.writeValue(writer, data);
      return writer.toString();
    } catch (JsonGenerationException e) {
      e.printStackTrace();
    } catch (JsonMappingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } 
    return null;
  }
  
  public static Map<String, Object> toObject(String json) {
    ObjectMapper mapper = new ObjectMapper();
    try {
      mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
      mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
      return (Map<String, Object>)mapper.readValue(json, new TypeReference<Map<String, Object>>() {
          
          });
    } catch (JsonParseException e) {
      e.printStackTrace();
    } catch (JsonMappingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } 
    return null;
  }
  
  public static Map<String, Object> toObject(InputStream json) {
    ObjectMapper mapper = new ObjectMapper();
    try {
      mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
      mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      return (Map<String, Object>)mapper.readValue(json, new TypeReference<Map<String, Object>>() {
          
          });
    } catch (JsonParseException e) {
      e.printStackTrace();
    } catch (JsonMappingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } 
    return null;
  }
  
  public static <T> T toObject(String json, Class<T> objType) {
    ObjectMapper mapper = new ObjectMapper();
    try {
      mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
      return (T)mapper.readValue(json, objType);
    } catch (JsonParseException e) {
      e.printStackTrace();
    } catch (JsonMappingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } 
    return null;
  }
  
  public static <T> T toObject(String json, TypeReference<T> typeRef) {
    ObjectMapper mapper = new ObjectMapper();
    try {
      mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
      return (T)mapper.readValue(json, typeRef);
    } catch (JsonParseException e) {
      e.printStackTrace();
    } catch (JsonMappingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } 
    return null;
  }
  
  public static <T> T toObject(InputStream json, Class<T> objType) {
    ObjectMapper mapper = new ObjectMapper();
    try {
      mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
      return (T)mapper.readValue(json, objType);
    } catch (JsonParseException e) {
      e.printStackTrace();
    } catch (JsonMappingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } 
    return null;
  }
  
  public static <T> T toObject(InputStream json, TypeReference<T> typeRef) {
    ObjectMapper mapper = new ObjectMapper();
    try {
      mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
      return (T)mapper.readValue(json, typeRef);
    } catch (JsonParseException e) {
      e.printStackTrace();
    } catch (JsonMappingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } 
    return null;
  }
}
